<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\controller\v1;

use app\api\controller\ApiBase;
use app\api\service\Image as ImageService;
use app\api\service\user\User as UserService;

class User extends ApiBase {

    /**
     * 当前用户详情
     */
    public function getDetail()
    {

        // 当前用户信息
        $userInfo = UserService::getCurrentLoginUser(true);
        $avatar = $userInfo['avatar'];
        if($avatar && (strpos($avatar, 'thumb_') !== false)){
            $userInfo['avatar'] = config('setting.domain_prefix').config('setting.image_prefix').'avatar/'.$avatar;
        }

        return success('查询成功', compact('userInfo'));
    }


    /**
     * 更新头像
     */
    public function updateAvatar(){
        $userId = UserService::getCurrentLoginUserId();
        $sImageService = new ImageService();
        $new_avatar = $sImageService->upload('avatar');
        $sUserService = new UserService();
        $data['avatar'] = $new_avatar;
        $res = $sUserService->edit($userId, $data);
        $new_avatar1 = config('setting.domain_prefix').config('setting.image_prefix').'avatar/'.$new_avatar;
        return success('更改成功', [
            'avatar' => $new_avatar1
        ]);
    }

    public function updateUserInfo(){
        $data = $this->postData('userInfo');
        $userId = UserService::getCurrentLoginUserId();
        $sUserService = new UserService();
        $res = $sUserService->edit($userId, $data);
        return success('更改成功');
    }

}